package cliente;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Cliente {
        
        private String ipServidor;
        private int puertoServidor = 4444;
        private Socket miSocket;
        private PrintWriter out ;
        private BufferedReader in ;
        
        Cliente(String ip){
            this.ipServidor = ip;
        }
        
        
        public void connect(){
            try {
                miSocket = new Socket(ipServidor, puertoServidor);
                out = new PrintWriter(miSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(miSocket.getInputStream()));
            } catch (UnknownHostException ex) {
                Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("No hay ningún servidor atendiendo por ese puerto");
            } catch (IOException ex) {
                Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
    
        public void setIpPuertoServidor(String ipServidor, int puertoServidor) {
                this.ipServidor = ipServidor;
                this.puertoServidor = puertoServidor;
        }

        public String ejecutarClass(String absolutePath, String nombreArchivo){
            String direccionesIp[] = null; 
            out.println("Cliente");
            out.flush();
            System.out.println("enviando informacion");
            out.println("Ejecutar");
            out.flush();
           
            try {
                direccionesIp = in.readLine().split("[|]");
            } catch (IOException ex) {
                Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            for(String ip: direccionesIp){
                 System.out.println(ip);
                 Thread hiloPorServ = null;
                 HiloEjecucionClass hiloPorServRun;
                  try {
                  hiloPorServRun = new HiloEjecucionClass(ip, absolutePath, nombreArchivo, "Ejecutar");
                  hiloPorServ = new Thread(hiloPorServRun);
                  hiloPorServ.start();
                } catch (Exception ex) {
                    Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return "";
        }


    public FileReader buscarResultado(String absolutePath, String nombreArchivo){
            String direccionesIp[] = null;
            out.println("Cliente");
            out.flush();
            System.out.println("enviando informacion");
            out.println("Busqueda");
            out.flush();

            try {
                direccionesIp = in.readLine().split("[|]");
            } catch (IOException ex) {
                Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
            }

            for(String ip: direccionesIp){
                 System.out.println(ip);
                 Thread hiloPorServ = null;
                 HiloEjecucionClass hiloPorServRun;
                  try {
                  hiloPorServRun = new HiloEjecucionClass(ip, absolutePath, nombreArchivo, "Busqueda");
                  hiloPorServ = new Thread(hiloPorServRun);
                  hiloPorServ.start();
                } catch (Exception ex) {
                    Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return null;
        }


        
        
        
        public void close(){
            try {
                out.close();
                in.close();
                miSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
}
